package com.troila.cloud.simplify.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.troila.cloud.simplify.config.WechatConfiguration.WechatProperties;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

@Configuration
@EnableConfigurationProperties(WechatProperties.class)
public class WechatConfiguration {

	@Autowired
	private WechatProperties wechat;

	@Bean
	public WxMpService getWechatService() {
		WxMpInMemoryConfigStorage storage = new WxMpInMemoryConfigStorage();
		storage.setAppId(wechat.getAppid());
		storage.setSecret(wechat.getSecret());
		storage.setToken(wechat.getToken());
		WxMpService svc = new WxMpServiceImpl();
		svc.setWxMpConfigStorage(storage);
		return svc;
	}

	@ConfigurationProperties(prefix = "wechat")
	public static class WechatProperties {
		private String appid;
		private String secret;
		private String token;

		public String getAppid() {
			return appid;
		}

		public void setAppid(String appid) {
			this.appid = appid;
		}

		public String getSecret() {
			return secret;
		}

		public void setSecret(String secret) {
			this.secret = secret;
		}

		public String getToken() {
			return token;
		}

		public void setToken(String token) {
			this.token = token;
		}

	}

}
